package com.jd.after.modules.mis.controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jd.after.common.core.P;
import com.jd.after.common.core.R;
import com.jd.after.modules.mis.entity.Homework;
import com.jd.after.modules.mis.service.HomeworkService;
import com.jd.after.modules.sys.service.DictService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController
 * @RequestMapping
 * 
 */
@RestController
@RequestMapping("/homework")
public class HomeworkController {

    @Autowired
    HomeworkService homeworkService;
    @Autowired
    DictService dictService;

    // @Autowired
    // private HttpServletRequest request; //自动注入request

    // http://localhost:8082/homework/list
    @PostMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {

        // QueryWrapper

        QueryWrapper<Homework> Q = new QueryWrapper<Homework>();

        String query = (String) params.get("query");

        if (query != null && !query.isEmpty())
            Q.like("hname", query);

        Q.orderByDesc("commit_time");

        IPage<Homework> list = homeworkService.page(P.page(params), Q);

        // 获取字典名称,将作业ID替换成字典
        for (Homework h : list.getRecords()) {
            h.setDict(dictService.getById(h.getHomeType()));
        }

        // int a=4/0;
        return R.ok(list);
    }

    /**
     * get
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        Homework homework = homeworkService.getById(id);
        return R.ok(homework);
    }

    /**
     * save
     * 
     * @Validated 配合后端验证
     * 
     * 
     * @param blog
     * @return
     */
    @PostMapping("/save")
    public R save(@Validated @RequestBody Homework homework) {
        homework.setCommitTime(LocalDateTime.now());
        // int a=4/0;
        homeworkService.save(homework);
        return R.ok();
    }

    /**
     * 
     * 
     * @param blog
     * @return
     */
    @PostMapping("/update")
    public R update(@Validated @RequestBody Homework homework) {
        homeworkService.updateById(homework);
        return R.ok();
    }

    /**
     * 
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        System.out.println(ids);
        homeworkService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
